package jschtest;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class ThreadPoolUtil {

    private static int DEFAULT_CORE_SIZE;

    private static int MAX_QUEUE_SIZE;

    private volatile static ThreadPoolExecutor executor;

    private ThreadPoolUtil(){

    }

    public static ThreadPoolExecutor getInstance(int size){
        DEFAULT_CORE_SIZE = size;
        MAX_QUEUE_SIZE = size;
        if(executor == null){
            synchronized (ThreadPoolUtil.class){
                executor = new ThreadPoolExecutor(DEFAULT_CORE_SIZE,
                        MAX_QUEUE_SIZE,
                        Integer.MAX_VALUE,
                        TimeUnit.SECONDS,
                        new LinkedBlockingDeque<Runnable>(Integer.MAX_VALUE),
                        Executors.defaultThreadFactory());
            }
        }
        return executor;
    }

    public void execute(Runnable runnable){
        if(runnable == null){
            return;
        }
        executor.execute(runnable);
    }

}
